import { defineConfig } from '@rsbuild/core';
import { pluginReact } from '@rsbuild/plugin-react';
import { pluginTypeCheck } from '@rsbuild/plugin-type-check';
import { pluginModuleFederation } from '@module-federation/rsbuild-plugin';
import { DEPLOY_CONFIG, isProduction } from '../../config/env';
import { getProxyContent } from '../../config/getProxyContent';
import { pluginLess } from '@rsbuild/plugin-less';
// @ts-ignore
import autoprefixer from 'autoprefixer';
// @ts-ignore
import pxtorem from 'postcss-pxtorem';

const proxy = 'https://desk.szisd.com.cn';

export default defineConfig({
  plugins: [
    pluginLess(),
    pluginReact(),
    pluginTypeCheck(),
    pluginModuleFederation({
      name: 'microApp', // 微应用名称
      filename: 'remoteEntry.js',
      exposes: {
        './Routes': './src/app/routes/AppRoutes', // 暴露路由组件
      },
      shared: {
        react: {
          singleton: true,
          eager: true,
          requiredVersion: '^18.2.0',
        },
        'react-dom': {
          singleton: true,
          eager: true,
          requiredVersion: '^18.2.0',
        },
        'react-router-dom': {
          singleton: true,
          eager: true,
          requiredVersion: '^6.8.0',
        },
        antd: {
          singleton: true,
          eager: true,
          requiredVersion: '^5.0.0',
        },
        '@shared/utils': {
          singleton: true,
          eager: true,
        },
      },
    })
  ],
  html: {
    template: '../../templates/mobile.html',
  },
  source: {
    entry: {
      index: './src/main.tsx',
    },
    include: [/[\\/]node_modules[\\/]@shared[\\/]utils[\\/]/],
    transformImport: [
      // {
      //   libraryName: '@shared/mobile',
      //   libraryDirectory: '',
      //   style: false,
      //   camelToDashComponentName: false
      // },
      {
        libraryName: '@shared/utils',
        libraryDirectory: 'lib',
        style: false,
        camelToDashComponentName: false,
        transformToDefaultImport: false
      },
      {
        libraryName: 'antd-mobile',
        libraryDirectory: 'es/components',
        style: false
      }
    ]
  },
  server: {
    port: 3001,
    proxy: {
      context: getProxyContent(),
      target: proxy,
      changeOrigin: true,
      headers: {
        origin: proxy,
        referer: proxy
      }
    }
  },
  tools: {
    postcss: {
      postcssOptions: (loaderContext) => {
        if (/node_modules[\/\\]antd-mobile/.test(loaderContext.resourcePath)) {
          return {
            config: true,
            plugins: [
              autoprefixer({
                browsers: ['last 2 versions', 'Firefox ESR', '> 1%', 'ie >= 8', 'iOS >= 8', 'Android >= 4'],
              }),
              pxtorem({
                // rootValue: 17,
                propWhiteList: []
              })
            ]
          }
        }
        if (/node_modules[\/\\]ehome-rcm[\/\\]es[\/\\]blank/.test(loaderContext.resourcePath)) {
          return {
            config: true,
            plugins: [
              autoprefixer({
                browsers: ['last 2 versions', 'Firefox ESR', '> 1%', 'ie >= 8', 'iOS >= 8', 'Android >= 4'],
              }),
              pxtorem({
                rootValue: 32,
                propWhiteList: []
              })
            ]
          }
        }
        return {
          config: true,
          plugins: [
            autoprefixer({
              browsers: ['last 2 versions', 'Firefox ESR', '> 1%', 'ie >= 8', 'iOS >= 8', 'Android >= 4'],
            }),
            pxtorem({
              // rootValue: 17,
              propWhiteList: []
            })
          ]
        }
      }
    }
  },
  output: {
    distPath: {
      root: 'dist',
    },
    // 设置微应用的资源路径前缀
    assetPrefix: isProduction ? `${DEPLOY_CONFIG.MICRO_APP_PATH}/` : '/',
  },
});
